/**
 * Created by gwzha on 26/12/18.
 */

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function formatTime(time, option) {
  time = +time * 1000
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

// 计算Task状态
export function computeTaskStatus (taskInfo) {
  const computedStatus = {}
  switch (taskInfo.state) {
    case 'CREATED':
      computedStatus.val = '已创建'
      computedStatus.className = 'status-wait'
      break
    case 'TAGING':
      computedStatus.val = '提交中'
      computedStatus.className = 'status-wait'
      break
    case 'TAGFAILED':
      computedStatus.val = '提交失败'
      computedStatus.className = 'status-failure'
      break
    case 'TAGSUCCESS':
      computedStatus.val = '提交成功'
      computedStatus.className = 'status-wait'
      break
    case 'UNDERPOWERED':
      computedStatus.val = '算力不足'
      computedStatus.className = 'status-failure'
      break
    case 'PRERUNNING': case 'WAITING':
      computedStatus.val = '等待'
      computedStatus.className = 'status-wait'
      break
    case 'STOPPING':
      computedStatus.val = '暂停中'
      computedStatus.className = 'status-wait'
      break
    case 'STOPPED':
      computedStatus.val = '暂停'
      computedStatus.className = 'status-stop'
      break
    case 'RUNNING':
      computedStatus.val = '运行中'
      computedStatus.className = 'status-run'
      break
    case 'SUCCEEDED':
      computedStatus.val = '成功'
      computedStatus.className = 'status-success'
      break
    case 'FAILED':
      computedStatus.val = '失败'
      computedStatus.className = 'status-failure'
      break
    case 'RESTARTING':
      computedStatus.val = '重启'
      computedStatus.className = 'status-wait'
      break
    case 'UNKNOWN' : default:
      computedStatus.val = 'N/A'
      computedStatus.className = 'status-wait'
  }
  return computedStatus
}

// 计算相差时间
export function computedDuration(startTime, endTime) {
  if (startTime) {
    if (startTime === null) {
      return 0
    }
    if (endTime === null) {
      endTime = Date.now()
    }
    const elapsedTime = Math.round(Math.max(0, endTime - startTime) / 1000)
    let result = ''
    const elapsedDay = parseInt(elapsedTime / (24 * 60 * 60))
    if (elapsedDay > 0) {
      result += elapsedDay + 'd '
    }
    const elapsedHour = parseInt((elapsedTime % (24 * 60 * 60)) / (60 * 60))
    if (result !== '' || (result === '' && elapsedHour > 0)) {
      result += elapsedHour + 'h '
    }
    const elapsedMinute = parseInt(elapsedTime % (60 * 60) / 60)
    if (result !== '' || (result === '' && elapsedMinute > 0)) {
      result += elapsedMinute + 'm '
    }
    const elapsedSecond = parseInt(elapsedTime % 60)
    result += elapsedSecond + 's'
    return result
  } else {
    return '--'
  }
}
